limsEnvironmentRoom.editPage = {

    getGridParam: function () {
        var _this = this
        return {
            type: 'grid',
            id: _this.idManager.roomGrid,
            url: _this.urlManager.roomGrid + '/queries',
            service: 'limsEnvironmentRoomServiceImpl',
            dbTable: 'T_LIMS_ENVIRONMENT_ROOM',
            columns: limsEnvironmentRoom.getEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    _this.refreshTabs(null)
                }
            },
            onRowActive: function (index, row) {
                _this.refreshTabs(row)
            },
            deleteFormatter: function (row) {
                return row.roomCode || row.id
            },
            page: true,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    Gikam.preInsert({
                        modalTitle: 'BASEMODULE.RM.ENVIRONMENTS.MODAL.ADD',
                        fields: limsEnvironmentRoom.getBaseInfoFormFields(),
                        url: _this.urlManager.roomGrid
                    }).done(function (id) {
                        Gikam.getComp(_this.idManager.roomGrid).refresh()
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp(_this.idManager.roomGrid).deleteRows(_this.urlManager.roomGrid)
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.EXPORT_TEMPLATE',
                icon: 'export',
                onClick: function () {
                    Gikam.getText(IFM_CONTEXT + '/core/module/item/files/template-urls' + Gikam.param({
                        templateCode: 'LimsEnvironmentRoomExportTemplate',
                        service: 'LimsEnvironmentRoomExportTemplate'
                    })).done(function (url) {
                        Gikam.download(url)
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.IMPORT',
                icon: 'upload',
                onClick: function () {
                    Gikam.create('simpleUploader', {
                        id: 'er-info-import',
                        dbTable: 'T_LIMS_ENVIRONMENT_ROOM',
                        autoClose: true,
                        multiple: false,
                        onAfterClose: function (fields) {
                            if (Gikam.isNotEmpty(fields)) {
                                workspace.window.showMask()
                                var url = Gikam.printf(IFM_CONTEXT + '/core/module/item/files/{id}/action/import/' + 'LimsEnvironmentRoomImportFileFunction', {
                                    id: fields[0].id
                                })
                                Gikam.post(url).done(function (req) {
                                    Gikam.getComp(_this.idManager.roomGrid).refresh()
                                    if (req.message) {
                                        Gikam.alert(req.message)
                                    }
                                    if (req.url) {
                                        Gikam.download(req.url);
                                    }
                                }).always(function () {
                                    workspace.window.closeMask()
                                })
                            }
                        }
                    })
                }
            }]
        }
    },

    refreshTabs: function (row) {
        let _this = this
        Gikam.getAsyncComp(_this.idManager.roomDataGrid).done(grid => {
            if (row && row.id) {
                grid.refresh({
                    url: _this.urlManager.roomDataGrid + '/queries',
                    requestData: {
                        roomId_EQ: row.id
                    }
                })
            } else {
                grid.refresh({
                    url: _this.urlManager.roomDataGrid + '/queries',
                    requestData: {
                        roomId_EQ: -1
                    }
                })
            }
        })
        Gikam.getAsyncComp(_this.idManager.roomDataGridSubmitted).done(grid => {
            if (row && row.id) {
                grid.refresh({
                    url: _this.urlManager.roomDataGrid + '/queries',
                    requestData: {
                        roomId_EQ: row.id
                    }
                })
            } else {
                grid.refresh({
                    url: _this.urlManager.roomDataGrid + '/queries',
                    requestData: {
                        roomId_EQ: -1
                    }
                })
            }
        })
        _this.refreshAttachments(_this.idManager.roomGrid)
    },

    getUnSubmitRoomDataGrid: function () {
        let _this = this
        return {
            type: 'grid',
            id: _this.idManager.roomDataGrid,
            service: 'limsEnvironmentDataServiceImpl',
            dbTable: 'T_LIMS_ENVIRONMENT_DATA',
            columns: limsEnvironmentRoom.getRoomDataEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                }
            },
            onRowActive: function (index, row) {
            },
            deleteFormatter: function (row) {
                return row.index + 1 || row.id
            },
            requestData: {
                processStatus_EQ: 'draft'
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    let roomGrid = Gikam.getComp(_this.idManager.roomGrid)
                    let actRow = roomGrid.getActivedRow()
                    if (Gikam.isEmpty(actRow)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                        return
                    }
                    Gikam.preInsert({
                        modalTitle: 'BASEMODULE.RM.ENVIRONMENTS.MODAL.ADD',
                        fields: limsEnvironmentRoom.getRoomDataBaseInfoFormFields(),
                        url: _this.urlManager.roomDataGrid,
                        param: {
                            roomId: actRow.id
                        }
                    }).done(function (id) {
                        Gikam.getComp(_this.idManager.roomDataGrid).refresh()
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp(_this.idManager.roomDataGrid).deleteRows(_this.urlManager.roomDataGrid)
                }
            }, {
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                icon: 'submit', color: 'success',
                onClick: function () {
                    let url = _this.urlManager.roomDataGrid + '/change/status'
                    let grid = Gikam.getComp(_this.idManager.roomDataGrid)
                    var selections = grid.getSelections()
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM')
                        return
                    }
                    var timeEmpty = selections.filter(r => {
                        return !r.time || r.time === ''
                    })
                    if (Gikam.isEmpty(timeEmpty)) {
                        Gikam.confirm('GIKAM.MODAL.SUBMIT', 'GIKAM.TIP.SUBMIT', function () {
                            Gikam.postText(url, Gikam.getJsonWrapper(null, [null, selections])).done(function (res) {
                                grid.refresh()
                            })
                        })
                    } else {
                        Gikam.alert('BASEMODULE.RM.ENVIRONMENTS.DATA.TIP.TIME_NOT_NULL')
                    }
                }
            }]
        }
    },

    getSubmittedRoomDataGrid: function () {
        let _this = this
        return {
            type: 'grid',
            id: _this.idManager.roomDataGridSubmitted,
            service: 'limsEnvironmentDataServiceImpl',
            dbTable: 'T_LIMS_ENVIRONMENT_DATA',
            columns: limsEnvironmentRoom.getRoomDataShowListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                }
            },
            onRowActive: function (index, row) {

            },
            requestData: {
                processStatus_EQ: 'done'
            },
            toolbar: []
        }
    },

    getAttachUploaderId: function (gridId) {
        return gridId + '-uploader'
    },

    refreshAttachments: function (gridId) {
        let _this = this
        let grid = Gikam.getComp(gridId)
        let actRow = grid.getActivedRow()
        Gikam.getAsyncComp(_this.getAttachUploaderId(gridId)).done(uploader => {
            if (actRow && actRow.id) {
                uploader.setOptions({
                    bizId: actRow.id,
                })
            } else {
                uploader.setOptions({
                    bizId: -1,
                })
            }
        })
    },

    getAttachmantStruct: function (gridId, dbTable) {
        let _this = this
        return {
            type: 'uploader',
            immediateUpload: true,
            id: _this.getAttachUploaderId(gridId),
            dbTable: dbTable,
            // bizId: -1,
            filter: false,
            genericQuery: false,
            onBeforeUpload: function () {
                var activedRow = Gikam.getComp(gridId).getActivedRow()
                if (Gikam.isEmpty(activedRow)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                    return false
                }
            },
        }
    },

    getBaseInfoTab: function () {
        var _this = this
        var first = false
        return {
            type: 'tab',
            id: 'main-environment-data-tab',
            panels: [{
                title: 'BASEMODULE.RM.ENVIRONMENTS.TAB.UN_SUBMIT_TAB',
                items: [_this.getUnSubmitRoomDataGrid()]
            }, {
                title: 'BASEMODULE.RM.ENVIRONMENTS.TAB.SUBMITTED_TAB',
                items: [_this.getSubmittedRoomDataGrid()]
            }, {
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [_this.getAttachmantStruct(_this.idManager.roomGrid, 'T_LIMS_ENVIRONMENT_ROOM')]
            }],
            onActived: function (data) {
                var tab = Gikam.getComp('main-environment-data-tab')
                switch (data.index) {
                    case 0:
                        if (first) {  // 首次加载页面时，不操作
                            Gikam.getAsyncComp(_this.idManager.roomDataGrid).done(function (grid) {
                                grid.refresh()
                            })
                        }
                        break
                    case 1:
                        Gikam.getAsyncComp(_this.idManager.roomDataGridSubmitted).done(function (grid) {
                            grid.refresh()
                        })
                        break
                }
                first = true
            }
        }
    },

    idManager: {
        roomGrid: 'lims-environment-room-edit-list-grid',
        roomDataGrid: 'lims-environment-data-edit-list-grid',
        roomDataGridSubmitted: 'lims-environment-data-edit-list-grid-submitted',
    },

    urlManager: {
        roomGrid: limsEnvironmentRoom.baseUrl,
        roomDataGrid: IFM_CONTEXT + '/secure/basemodule/rm/environment-datas'
    },

    create: function () {
        var _this = this
        Gikam.create('layout', {
            id: 'lims-environment-room-edit-list-layout',
            renderTo: workspace.window.$dom,
            west: {
                width: '40%',
                items: [this.getGridParam()]
            },
            center: {
                items: [this.getBaseInfoTab()]
            }
        })
    },

    init: function () {
        this.create()
    }
}
